package com.meitianhui.convenitenbuy.module.wallet.presenter;

import android.text.TextUtils;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletPaymentSettingContract;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletPaymentSettingActivity;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.SystemUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletPaymentSettingPresenter implements WalletPaymentSettingContract.Presenter {

    private WalletPaymentSettingActivity context;
    private final WalletPaymentSettingContract.View view;

    public WalletPaymentSettingPresenter(WalletPaymentSettingActivity view,
                                         WalletPaymentSettingActivity context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void checkInput(String input) {
        SystemUtils.hidKeyBoard(context);

        if (TextUtils.isEmpty(input)) {
            view.checkResult(Constants.WALLET_PAYMENT_SETTING_EMPTY);
            return;
        } else if (!StringUtils.isAmount(input)) {
            view.checkResult(Constants.WALLET_PAYMENT_SETTING_WRONG);
            return;
        } else if ("0".equals(input)
                || "0.".equals(input)
                || "0.0".equals(input)
                || "0.00".equals(input)) {
            view.checkResult(Constants.WALLET_PAYMENT_SETTING_WRONG);
            return;
        }

        BigDecimal number = new BigDecimal(input);
        // 大数据比较
        if (number.compareTo(new BigDecimal("0.00")) == 0) {
            view.checkResult(Constants.WALLET_PAYMENT_SETTING_WRONG);
            return;
        }

        String money = InfoProvider.getUserAsset(App.getInstance()).getCash_balance();
        if (Float.parseFloat(input) > Float.parseFloat(money)) {
            view.checkResult(Constants.WALLET_PAYMENT_SETTING_ERROR);
            return;
        }

        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String inputMoney = decimalFormat.format(Double.valueOf(input));
        UserToken userToken = InfoProvider.getUserToken(App.getInstance());
        if (userToken.getSmall_direct_flag().equals("N")
                && userToken.getPayment_password_flag().equals("Y")) {
            view.checkPassWord(inputMoney);
            return;

        } else if (userToken.getSmall_direct_flag().equals("Y")
                && Float.parseFloat(input) > Constants.SMALL_AMOUNT
                && userToken.getPayment_password_flag().equals("Y")) {
            view.checkPassWord(inputMoney);
            return;
        }

        view.checkResultSuccess(inputMoney);
    }

}
